package com.rgs.se.concurrent.blockingQueue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Ren Gaoshuai
 * @ClassName producer
 * @Description 生产者
 * @date 2021-09-01 22:32
 **/
public class Producer implements Runnable {

    private volatile boolean isRunning = true;
    private BlockingQueue queue;
    private static AtomicInteger count = new AtomicInteger();

    public Producer(BlockingQueue queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        String data = null;
        System.out.println("启动生产者线程。");
        try {
            data = "data:" + count.incrementAndGet();
            if (!queue.offer(data)) {
                System.out.println("放入数据失败");
            }
        } finally {
            System.out.println("退出生产者线程。");
        }

    }

    public void stop() {
        isRunning = false;
    }
}
